package com.atguigu.Controller;

import com.atguigu.result.RetVal;
import com.atguigu.search.Product;
import com.atguigu.search.SearchParam;
import com.atguigu.search.SearchResponseVo;
import com.atguigu.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private ElasticsearchRestTemplate esRestTemplate;
    @Autowired
    private SearchService searchService;

    //1.创建索引
    @GetMapping("createIndex")
    public RetVal createIndex() {
        // 调用类中方法自动创建
        esRestTemplate.createIndex(Product.class);
        esRestTemplate.putMapping(Product.class);
        return RetVal.ok();
    }

    //上架
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId){
        searchService.onSale(skuId);
        return RetVal.ok();
    }

    //下架
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId){
        searchService.offSale(skuId);
        return RetVal.ok();
    }

    //4.商品的热度排名
    @GetMapping("incrHotScore/{skuId}")
    public RetVal incrHotScore(@PathVariable Long skuId){
        searchService.inctHotScore(skuId);
        return RetVal.ok();
    }

    @PostMapping
    public RetVal searchParam(@RequestBody SearchParam searchParam){
        SearchResponseVo searchResponseVo =searchService.searchParam(searchParam);
        return RetVal.ok(searchResponseVo);
    }

}
